//
//  NewFeatureViewController.swift
//  SinaBlog
//
//  Created by macs on 15/11/12.
//  Copyright © 2015年 BlackTail. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

private let indexCount = 4

class NewFeatureViewController: UICollectionViewController {
    
    init() {
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .Horizontal
        super.init(collectionViewLayout: layout);
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
    }
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return indexCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        
        // Configure the cell
        //        cell.backgroundColor = indexPath.item % 2 == 1 ? UIColor.redColor() : UIColor.blueColor()
        cell.index = indexPath.item
        return cell
    }
    
    //    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    //
    //        let page = Int(scrollView.contentOffset.x / screenW)
    //
    //        if page == indexCount - 1 {
    //
    //            if let cell = collectionView?.visibleCells().last as? NewFeatureCell
    //            {
    //                cell.startAnimation()
    //            }
    //        }
    //    }
    
//    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
//        
//        
//        
//        if let path = collectionView.indexPathsForVisibleItems().last where path.item == indexCount - 1 {
//            let cell = collectionView.cellForItemAtIndexPath(path) as! NewFeatureCell
//            
//            cell.startAnimation()
//        }
//        
//    }
    
}

class NewFeatureCell : UICollectionViewCell {
    
    var index : Int = 0 {
        didSet {
            
            if index == 3 {
                
                startBtn.hidden = false
                //调用动画时机两种方式
                                startAnimation()
            } else {
                startBtn.hidden = true
                
            }
            
            iconView.image = UIImage(named: "new_feature_" + String(index + 1))
        }
    }
    
    @objc private func startBtnDidClick() {
        
        NSNotificationCenter.defaultCenter().postNotificationName(notityKey, object: nil, userInfo: nil)
        
    }
    
    func startAnimation() {
        
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            
            self.startBtn.transform = CGAffineTransformMakeScale(1, 1)
            self.startBtn.hidden = false
            }) { (_) -> Void in
                
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //2.添加子控件,布局
    private func setupUI () {
        
        contentView.addSubview(iconView)
        contentView.addSubview(startBtn)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
        
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView.snp_centerX)
            make.bottom.equalTo(contentView.snp_bottom).offset(-200)
        }
        startBtn.addTarget(self, action: "startBtnDidClick", forControlEvents: .TouchUpInside)
    }
    
    //1.懒加载子控件
    lazy var iconView = UIImageView()
    
    lazy var startBtn : UIButton = UIButton(title: "开始体验", color: nil, icon: "new_feature_finish_button")
    
}


